﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using heyirunde.wx;

namespace heyirunde.wx
{
    /// <summary>
    /// Get_ticket 的摘要说明
    /// </summary>
    public class Get_ticket : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            JsonHelper jsonHelper = new JsonHelper();

            using (var db = new SqlSugarClient(DBHelper.connectionString))
            {
             
               
                 

                    string interfaceUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
                    
                    RestClient client = new RestClient(interfaceUrl);
                    RestRequest request1 = new RestRequest();
                    request1.Method = Method.GET;
                    request1.AddQueryParameter("access_token",wxconfig.getToken());
                    request1.AddQueryParameter("type", "jsapi");
                   
                    IRestResponse request2 = client.Execute(request1);

                    if (request2.StatusCode.ToString() == "OK")
                    {
                        JObject objs = (JObject)JsonConvert.DeserializeObject(request2.Content);
                        context.Response.Write(jsonHelper.ObjToJson(new { state = 200, msg = "成功", items = objs["ticket"] }));
                        return;
                    }

                
            }   
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


    }
}